package com.xiaofeng.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.xiaofeng.common.Const;
import com.xiaofeng.pojo.Transfer;
import com.xiaofeng.service.iservice.TransferService;
@Controller("transferAction")
public class TransferAction implements ModelDriven<Transfer>,RequestAware,SessionAware{
	@Autowired
	private TransferService ts;
	
	private int page;
	

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}
	private Map<String, Object> sessionMap;
	@Override
	public void setSession(Map<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
		
	}

	private Map<String, Object> requestMap;
	@Override
	public void setRequest(Map<String, Object> requestMap) {
		this.requestMap = requestMap;
		
	}

	private Transfer transfer = new Transfer();
	@Override
	public Transfer getModel() {
		
		return transfer;
	}

	public String save(){
		return ts.insert(transfer);
	}
	public String delete(){
		return ts.delete(transfer);	
	}
	public String update(){
		return ts.update(transfer);
	}
	public String findAll(){
		int page = 1;
		int num = 1;
		int size = 3;
		String msg = Const.ERROR;
		List<Transfer> list = ts.findAll(num,size);
		if(list !=null && list.size() > 0){
			requestMap.put("list", list);
		requestMap.put("page", page);
			msg = Const.SUCCESS;
		}
		return msg;
	}
	public String findpage(){
		String msg = Const.ERROR;		
		List<Transfer> list = ts.findByPageNo(page).getList();
		System.out.println(list);
		page = ts.findByPageNo(page).getPageNo();
		if(list != null && list.size() > 0){
			requestMap.put("list", list);
		
			requestMap.put("page", page);
		}
		msg = Const.SUCCESS;				
		return msg;
		
	}
	
	public String findById(){
		String msg = Const.ERROR;
		Transfer t = ts.findById(transfer.getId());
		if(t != null){
			requestMap.put("transfer", t);
			msg = Const.SUCCESS;
		}
		return msg;
		
	}
	public String load(){
		System.out.println("---------------");
		String msg = Const.ERROR;
		System.out.println("---------------");
		Transfer t = ts.findById(transfer.getId());
		System.out.println("t=" + t);
		if(t != null){
			requestMap.put("transfer", t);
			msg = Const.SUCCESS;
		}
		return msg;
	}
}
